package com.example.exp06_data_resolution;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.widget.EditText;
import android.widget.RadioGroup;

public class MainActivity extends AppCompatActivity {
    private EditText editText1,editText2;
    private RadioGroup radioGroup;

    SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editText1 = findViewById(R.id.editText_name);
        editText2 = findViewById(R.id.editText_city);
        radioGroup = findViewById(R.id.rgp);

        //获取SharedPreferences实例
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);

        //获取SharedPreferences实例的另一种方法
        //SharedPreferences sharedPreferences = getSharedPreferences("myData",MODE_APPEND);

        //获取Editor实例
        editor = sharedPreferences.edit();

        //使用editor进行不同类型的信息存储
        editor.putString("name","LuoQianshi");
        editor.putString("city","Amy");
        editor.putInt("R.id.girlbutton",R.id.girlbutton);

        //提交生效
        editor.commit();

        //提取默认数据
        editText1.setText(sharedPreferences.getString("name","无名"));
        editText2.setText(sharedPreferences.getString("city","默认城市是厦门！"));
        radioGroup.check(sharedPreferences.getInt("R.id.girlbutton",R.id.boybutton));
    }
}